package com.android.hw.projectmenu;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.CharBuffer;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.os.SystemProperties;

public class DiagSwitch extends Activity implements RadioGroup.OnCheckedChangeListener{
	private RadioButton switch_on;
	private RadioButton switch_off;
	private RadioButton qcomswitch;
	private RadioGroup test_switch;
	private final String configPath="//sys//class//android_usb//android0//device_mode";
	private boolean mRebooting = false;
	private String USER_SWITCH = "userswitch";
	private String TEST_SWITCH = "testswitch";
	private String QCOM_SWITCH = "qcomswitch";
	private String TAG = "DiagSwitch";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.diag_switch);
		/*15-09-23 gaoshenpan not change USB when monkeyTest begin*/
		if(isMonkeyRunning()){
			this.finish();
		}
		/*15-09-23 gaoshenpan not change USB when monkeyTest end*/
		switch_on=(RadioButton)findViewById(R.id.switch_on);
		switch_off=(RadioButton)findViewById(R.id.switch_off);
		qcomswitch=(RadioButton)findViewById(R.id.qcomswitch);
		test_switch=(RadioGroup)findViewById(R.id.test_switch);
		updateView();
		test_switch.setOnCheckedChangeListener(this);
	}
	/*15-09-23 gaoshenpan not change USB when monkeyTest begin*/
	public static boolean isMonkeyRunning() {
		return ActivityManager.isUserAMonkey();
	}
	/*15-09-23 gaoshenpan not change USB when monkeyTest end*/

	@Override
	protected void onResume() {
		super.onResume();
		if (mRebooting) {
			return;
		}
		updateView();
	}
    
	public void updateView(){
		/*gaoshenpan 15-8-8 fix add qcom_switch begin*/
		switch (isTestSwitchOn()) {
		case 0:
			switch_off.setChecked(true);
			break;
		case 1:
			switch_on.setChecked(true) ;
			break;
		case 2:
			qcomswitch.setChecked(true) ;
			break;

		default:
			break;
		}
		/*gaoshenpan 15-8-8 fix add qcom_switch end*/
	}
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch(checkedId){
		case R.id.switch_on:
			configDialog(TEST_SWITCH);
			break;
		case R.id.switch_off:
			configDialog(USER_SWITCH);
			/*gaoshenpan 15-8-11 fix can not switch usermode begin*/
			break;
			/*gaoshenpan 15-8-11 fix can not switch usermode end*/
			/*gaoshenpan 15-8-8 fix add qcom_switch begin*/
		case R.id.qcomswitch:
			configDialog(QCOM_SWITCH);
			break;
			/*gaoshenpan 15-8-8 fix add qcom_switch end*/
		}
		
	}
	public void configDialog(final String reason){
		/*15-09-06 gaoshenpan fix not do reboot when DiagSwitch begin*/
		SystemProperties.set("diag.switch.reboot", "1");
		/*15-09-06 gaoshenpan fix not do reboot when DiagSwitch end*/
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.reboot_message)
		       .setCancelable(false)
		       .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
                               reboot(reason);
                               dialog.cancel();
		           }
		       })
		       .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		                finish();
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
	}

    private void reboot(String reason) {
    	Intent intent = new Intent(Intent.ACTION_REBOOT);
    	intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
        intent.putExtra(Intent.EXTRA_REASON, reason);
    	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    	startActivity(intent);
        mRebooting = true;
    }
    /*gaoshenpan 15-8-8 fix add qcom_switch begin*/
    public int isTestSwitchOn(){
        try {
            FileInputStream inStream=new FileInputStream(configPath);
            byte[] buffer= new byte[1];
		    int nu=inStream.read(buffer);
            inStream.close();
            Log.e(TAG, "nu=" + nu);
            Log.e(TAG, "buffer.toString()=" + new String(buffer));
		    if (nu>=0){
		    	if("1".equals(new String(buffer))){
		    		return 1;
		    	}else if("2".equals(new String(buffer))) {
					return 2;
				}else if("0".equals(new String(buffer))) {
					return 0;
				}
		    }else {
		    	return 1;
			}
			
    	} catch (FileNotFoundException e) {
	    	e.printStackTrace();
            // default test mode without usb_mode file. 
            return 1;
    	} catch (IOException e) {
	    	e.printStackTrace();
	    }
    	return 1;
   }
    /*gaoshenpan 15-8-8 fix add qcom_switch end*/
  
  public boolean storeToFile(int i) {
	  try {
			FileOutputStream outStream = new FileOutputStream(configPath);
			String s = (i == 0 ? "0" : "1");
			Log.e(TAG, "s=" + s);
			outStream.write(s.getBytes());
			outStream.close();
			return true;
		} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	return false;
  }
   

}
